//
//  SXTopicListTableViewCell.m
//  ShiXun
//
//  Created by lanou on 15/10/27.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXTopicListTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "UIView+UIView.h"
#import "SXCollectionManager.h"
#import "UIButton+WebCache.h"
#import "MJPhoto.h"
#import "MJPhotoBrowser.h"

@implementation SXTopicListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _avatar_urlImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
        _avatar_urlImage.layer.masksToBounds = YES;
        _avatar_urlImage.layer.cornerRadius = 15;
        [self.contentView addSubview:_avatar_urlImage];
        
        _screen_nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 10, kScreenWidth / 2, 30)];
        _screen_nameLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_screen_nameLabel];
        
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, kScreenWidth - 20, 30)];
        _contentLabel.numberOfLines = 0;
        _contentLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_contentLabel];
        
        _shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        UIImageView *imageS = [[UIImageView alloc] initWithFrame:CGRectMake(0, 5, 30, 20)];
        imageS.image = [UIImage imageNamed:@"Topic-fenxiang"];
        [_shareButton addSubview:imageS];
        UILabel *labelS = [[UILabel alloc] initWithFrame:CGRectMake(35, 0, 50, 30)];
        labelS.text = @"转发";
        labelS.font = [UIFont systemFontOfSize:12];
        [_shareButton addSubview:labelS];
        [self.contentView addSubview:_shareButton];
        
        _collectionButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_collectionButton setBackgroundImage:[UIImage imageNamed:@"Topic-shoucang1"] forState:UIControlStateNormal];
        [_collectionButton setBackgroundImage:[UIImage imageNamed:@"SX-yuedushoucang1"] forState:UIControlStateSelected];
        [self.contentView addSubview:_collectionButton];
        
        _collectionB = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _collectionButton.selected = [SXCollectionManager checkCollectionWithTableName:@"topic" urlID:self.configureModel.thread_id];
        [_collectionB setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_collectionB setTitleColor:[UIColor blackColor] forState:(UIControlStateSelected)];
        [_collectionB setTitle:@"收藏" forState:(UIControlStateNormal)];
        [_collectionB setTitle:@"取消收藏" forState:(UIControlStateSelected)];
        _collectionB.titleLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:_collectionB];
    }
    return self;
}

// 赋值
- (void)setConfigureModel:(SXTopicListModel *)configureModel
{
    _configureModel = configureModel;
    [_avatar_urlImage sd_setImageWithURL:[NSURL URLWithString:_configureModel.userModel.avatar_url] placeholderImage:[UIImage imageNamed:@"SX-placeholder"]];
    _screen_nameLabel.text = _configureModel.userModel.screen_name;
    _contentLabel.text = _configureModel.content;
    _contentLabel.frame = CGRectMake(10, 50, kScreenWidth - 20, [SXTopicListTableViewCell heightOfContent:_contentLabel.text]);
    
    // 分享按钮
    _shareButton.frame = CGRectMake(10, [SXTopicListTableViewCell heightOfContent:_contentLabel.text] + 60, 90, 30);
    // 收藏按钮
    _collectionButton.frame = CGRectMake(kScreenWidth * 3/4, [SXTopicListTableViewCell heightOfContent:_contentLabel.text] + 63, 25, 25);
    _collectionB.frame = CGRectMake(kScreenWidth * 4/5 + 10, [SXTopicListTableViewCell heightOfContent:_contentLabel.text] + 60, 60, 30);

    [_View removeFromSuperview];
    if (configureModel.thumbImageArray.count > 0) {
        _View = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        [self.contentView addSubview:_View];
        if (configureModel.thumbImageArray.count == 1) {
            [_View removeAllSubviews];
            _View.frame = CGRectMake(0, 50 + _contentLabel.height, kScreenWidth, (kScreenWidth - 30) / 3 + 30);
            UIImageView *thumbImage = [[UIImageView alloc] initWithFrame:CGRectMake(10 , 0, (kScreenWidth - 30) / 3, (kScreenWidth - 30) / 3 + 30)];
            // 图片加载
            [thumbImage sd_setImageWithURL:[NSURL URLWithString:[configureModel.thumbImageArray[0] Imgurl]] placeholderImage:[UIImage imageNamed:@"SX-placeholder"]];
            // 点击放大
            thumbImage.tag = 0 + 1550;
            thumbImage.userInteractionEnabled = YES;
            UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(recognizer:)];
            [thumbImage addGestureRecognizer:recognizer];
            [_View addSubview:thumbImage];
            // 分享按钮
            _shareButton.frame = CGRectMake(10, [SXTopicListTableViewCell heightOfContent:_contentLabel.text] + 100 + (kScreenWidth - 30) / 3, 90, 30);
            // 收藏按钮
            _collectionButton.frame = CGRectMake(kScreenWidth * 3/4, [SXTopicListTableViewCell heightOfContent:_contentLabel.text] + 103 + (kScreenWidth - 30) / 3, 25, 25);
            _collectionB.frame = CGRectMake(kScreenWidth * 4/5 + 10, [SXTopicListTableViewCell heightOfContent:_contentLabel.text] + 100 + (kScreenWidth - 30) / 3, 50, 30);
        } else{
            _View.frame = CGRectMake(0, 50 + _contentLabel.height, kScreenWidth, (kScreenWidth - 30) / 3);
        for (int i = 0; i < configureModel.thumbImageArray.count; i++) {
            if (i < 3) {
                UIImageView *thumbImage = [[UIImageView alloc] initWithFrame:CGRectMake(10 + i * ((kScreenWidth - 30) / 3 + 5) , 0, (kScreenWidth - 30) / 3, (kScreenWidth - 30) / 3)];
                // 图片加载
                [thumbImage sd_setImageWithURL:[NSURL URLWithString:[configureModel.thumbImageArray[i] Imgurl]] placeholderImage:[UIImage imageNamed:@"SX-placeholder"]];
                // 点击放大
                thumbImage.tag = i + 1550;
                thumbImage.userInteractionEnabled = YES;
                UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(recognizer:)];
                [thumbImage addGestureRecognizer:recognizer];
                [_View addSubview:thumbImage];
                // 分享按钮
                _shareButton.frame = CGRectMake(10, [SXTopicListTableViewCell heightOfContent:_contentLabel.text] + 70 + (kScreenWidth - 30) / 3, 90, 30);
                // 收藏按钮
                _collectionButton.frame = CGRectMake(kScreenWidth * 3/4, [SXTopicListTableViewCell heightOfContent:_contentLabel.text] + 73 + (kScreenWidth - 30) / 3, 25, 25);
                _collectionB.frame = CGRectMake(kScreenWidth * 4/5 + 10, [SXTopicListTableViewCell heightOfContent:_contentLabel.text] + 70 + (kScreenWidth - 30) / 3, 50, 30);
            }else if (i > 2 && i < 6) {
                _View.frame = CGRectMake(0, 50 + _contentLabel.height, kScreenWidth, (kScreenWidth - 30) / 3 * 2 + 10);
                UIImageView *thumbImage = [[UIImageView alloc] initWithFrame:CGRectMake(10 + (i - 3) * ((kScreenWidth - 30) / 3 + 5) , 10 + (kScreenWidth - 30) / 3, (kScreenWidth - 30) / 3, (kScreenWidth - 30) / 3)];
                // 图片加载
                [thumbImage sd_setImageWithURL:[NSURL URLWithString:[configureModel.thumbImageArray[i] Imgurl]] placeholderImage:[UIImage imageNamed:@"SX-placeholder"]];
                // 点击放大
                thumbImage.tag = i + 1550;
                thumbImage.userInteractionEnabled = YES;
                UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(recognizer:)];
                [thumbImage addGestureRecognizer:recognizer];
                [_View addSubview:thumbImage];
                // 分享按钮
                _shareButton.frame = CGRectMake(10, [SXTopicListTableViewCell heightOfContent:_contentLabel.text] + 80 + (kScreenWidth - 30) / 3 * 2, 90, 30);
                // 收藏按钮
                _collectionButton.frame = CGRectMake(kScreenWidth * 3/4, [SXTopicListTableViewCell heightOfContent:_contentLabel.text] + 83 + (kScreenWidth - 30) / 3 * 2, 25, 25);
                _collectionB.frame = CGRectMake(kScreenWidth * 4/5 + 10, [SXTopicListTableViewCell heightOfContent:_contentLabel.text] + 80 + (kScreenWidth - 30) / 3 * 2, 50, 30);
            } else {
                _View.frame = CGRectMake(0, 50 + _contentLabel.height, kScreenWidth, (kScreenWidth - 30) / 3 * 3 + 20);
                UIImageView *thumbImage = [[UIImageView alloc] initWithFrame:CGRectMake(10 + (i - 6) * ((kScreenWidth - 30) / 3 + 5) , 20 + (kScreenWidth - 30) / 3 * 2, (kScreenWidth - 30) / 3, (kScreenWidth - 30) / 3)];
                // 图片加载
                [thumbImage sd_setImageWithURL:[NSURL URLWithString:[_configureModel.thumbImageArray[i] Imgurl]] placeholderImage:[UIImage imageNamed:@"SX-placeholder"]];
                // 点击放大
                thumbImage.tag = i + 1550;
                thumbImage.userInteractionEnabled = YES;
                UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(recognizer:)];
                [thumbImage addGestureRecognizer:recognizer];
                [_View addSubview:thumbImage];
                // 分享按钮
                _shareButton.frame = CGRectMake(10, [SXTopicListTableViewCell heightOfContent:_contentLabel.text] + 80 + (kScreenWidth - 30) / 3 * 3, 90, 30);
                // 收藏按钮
                _collectionButton.frame = CGRectMake(kScreenWidth * 3/4, [SXTopicListTableViewCell heightOfContent:_contentLabel.text] + 83 + (kScreenWidth - 30) / 3 * 3, 25, 25);
                _collectionB.frame = CGRectMake(kScreenWidth * 4/5 + 10, [SXTopicListTableViewCell heightOfContent:_contentLabel.text] + 80 + (kScreenWidth - 30) / 3 * 3, 50, 30);
            }
        }
    }
}
    [self.contentView setNeedsDisplay];
}
// 点击图片放大
- (void)recognizer:(UITapGestureRecognizer *)recognizer
{
    // 1.封装图片数据
    NSMutableArray *photos = [NSMutableArray arrayWithCapacity:_configureModel.thumbImageArray.count];
    for (int i = 0; i < _configureModel.thumbImageArray.count; i++) {
        // 替换为中等尺寸图片
        NSString *url = [[_configureModel.thumbImageArray[i] Imgurl] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
        MJPhoto *photo = [[MJPhoto alloc] init];
        photo.url = [NSURL URLWithString:url]; // 图片路径
        photo.srcImageView = (UIImageView*)[self.View viewWithTag:i + 1550]; // 来源于哪个UIImageView
        [photos addObject:photo];
    }
    // 2.显示相册
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    browser.currentPhotoIndex = recognizer.view.tag-1550; // 弹出相册时显示的第一张图片是？
    browser.photos = photos; // 设置所有的图片
    [browser show];
}

// 自适应高度
+ (CGFloat)heightOfContent:(NSString *)content
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16] forKey:NSFontAttributeName];
    CGRect bounds = [content boundingRectWithSize:CGSizeMake(kScreenWidth - 20, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    return bounds.size.height;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
